import logging
import requests
from requests import Session
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class YTSCardApi:
    def __init__(self):
        self.session = Session()
        self.prxy = {
            # "http": "http://127.0.0.1:8080",
            # "https": "https://127.0.0.1:8080",
        }

    def activate_card(self, data: str, token: str):
        url = 'https://gljm.ytsjq.com/mobile/order/activate-gift-coupon?partner_code=ezMPLGBZgDfvAodjhzJVsQ&siteChannelType=3&m_code=dwr1Op9C3C5qVSng3ZWSFA'

        response = self.session.post(
            url, data={"requestEncryptStr": data}, headers=self.config_headers(token), proxies=self.prxy)
        content_type = response.headers.get("content-type")
        if response.status_code == 200 and content_type and "application/json" in content_type:
            try:
                return response.json()

            except Exception as e:
                logger.error("Error parsing JSON response: %s", e)

        else:
            return response.content.decode()

    def get_product_info(self, product_price_id: int = 1781, site_channel_type: int = 3, partner_code: str = "ezMPLGBZgDfvAodjhzJVsQ", price_group_type: int = 3):
        url = f'https://gljm.ytsjq.com/product/partner-product-detail?partner_code={partner_code}&m_code=dwr1Op9C3C5qVSng3ZWSFA'
        '''
        productPriceId:  1781
        siteChannelType: 3
        partner_code:    ezMPLGBZgDfvAodjhzJVsQ
        priceGroupType:  3
        '''
        data = {
            "productPriceId":  product_price_id,
            "siteChannelType": site_channel_type,
            "partner_code":    partner_code,
            "priceGroupType":  price_group_type,
        }
        response = self.session.post(url, data=data, proxies=self.prxy)
        # print("res", response.cookies)
        return response.json(), response.cookies.get("_token")

    def config_headers(self, token):
        return {
            'Accept': 'application/json;charset=UTF-8',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'Cache-Control': 'private',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Host': 'gljm.ytsjq.com',
            'Referer': 'https://gljm.ytsjq.com/wapmall/index.html?m_code=dwr1Op9C3C5qVSng3ZWSFA',
            'Cookie': '_token=' + token,
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
            'lang': 'zh',
            'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'x-requested-with': 'XMLHttpRequest'
        }
